//
//  WmcAvatarCropVC.swift
//  YPWatermarkCamera
//
//  Created by yupao_ios_macmini06 on 2022/10/17.
//

import UIKit

// MARK: - 扩展WmcAvatarCropVC + WmcNavigatorProtocol

extension WmcAvatarCropVC: WmcNavigatorProtocol {
    static func navigationPaths() -> [WmcNavigatorManager.Path] {
        return [.avatarCropping]
    }

    static func willMakeNavigationVC(with parms: WmcNavigatorManager.Parms) -> UIViewController {
        var vm = WmcAvatarCropVM()
        if let viewModel = parms.context as? WmcAvatarCropVM {
            vm = viewModel
        }
        let vc = WmcAvatarCropVC(vm)
        return vc
    }
}

// MARK: - 扩展WmcAvatarCropVC + WmcVMProtocol

extension WmcAvatarCropVC: WmcVMProtocol {
    typealias VMType = WmcAvatarCropVM
}

// MARK: - [WmcAvatarCropVC]

class WmcAvatarCropVC: WmcBaseVC {
    // Lifecycle

    override func viewDidLoad() {
        super.viewDidLoad()

        /// 设置初始拉伸比例值
        
    }

    // Internal

    lazy var saveBtn: UIButton = {
        let btn = UIButton.Wmc_themeColorBtn(color: .wmc_mainTheme, title: "保存")
        return btn
    }()

    lazy var croppingRectView: UIView = {
        let view = UIView()
        return view
    }()

    lazy var panImageView: WmcPanZoomImageView = {
        let view = WmcPanZoomImageView(frame: CGRect(x: 0, y: Self.NavHeight, width: Wmc_screenWidth, height: Wmc_standardHeight - Self.NavHeight))
        return view
    }()

    lazy var maskView: WmcAvatarCropMask = {
        let view = WmcAvatarCropMask()
        return view
    }()

    override func makeUI() {
        view.backgroundColor = .black
        makeNavView(style: .dark)

        navView.addSubview(saveBtn)
        saveBtn.snp.makeConstraints { make in
            make.bottom.equalTo(-6)
            make.width.equalTo(52.wmc_autoHor)
            make.height.equalTo(32)
            make.right.equalTo(-Wmc_standardEdge.right)
        }

        addSubview(panImageView)
        addSubview(maskView)

        panImageView.snp.makeConstraints { make in
            make.top.equalTo(navView.snp.bottom)
            make.left.right.bottom.equalToSuperview()
        }
        maskView.snp.makeConstraints { make in
            make.top.left.right.bottom.equalTo(panImageView)
        }
        
        self.setScrollViewOffset()
    }

    override func bindViewModel() {
        self.panImageView.image = self.viewModel.image
    }

    override func bindViewAction() {
        saveBtn.yp.addEvent { [weak self] btn in
            guard let weakSelf = self, let b = weakSelf.viewModel.croppingImageBlock else { return }
            let cropRect = weakSelf.getImageCropRect()
            guard let croppedCGImage = weakSelf.viewModel.image?.cgImage?.cropping(to: cropRect) else { return }
            let croppedImage = UIImage(cgImage: croppedCGImage)
            b(croppedImage)
            weakSelf.popVC()
        }
    }

    // Private

    /// 裁剪图片
    /// - Returns: description
    private func getImageCropRect() -> CGRect {
        guard let image = viewModel.image else { return CGRect.zero }
        let imageScale: CGFloat = min(image.size.width / self.maskView.cropRect.size.width, image.size.height / self.maskView.cropRect.size.height)
        let zoomFactor = 1 / panImageView.zoomScale
        
        let inset = (self.panImageView.frame.height - maskView.cropRectView.frame.height) / 2
        let x = (panImageView.contentOffset.x + self.maskView.cropRect.origin.x) * zoomFactor * imageScale
        let y = (panImageView.contentOffset.y + self.maskView.cropRect.origin.y) * zoomFactor * imageScale - inset * panImageView.zoomScale
        let width = self.maskView.cropRect.size.width * zoomFactor * imageScale
        let height = self.maskView.cropRect.size.height * zoomFactor * imageScale
        return CGRect.init(x: x, y: y, width: width, height: height)
    }
    
    func setScrollViewOffset() -> Void {
        let inset = (self.panImageView.frame.height - maskView.cropRectView.frame.height) / 2
        self.panImageView.contentInset = UIEdgeInsets(top: inset, left: 0, bottom: inset, right: 0)
        self.panImageView.contentOffset = CGPoint(x: (maskView.cropRectView.frame.width - panImageView.frame.width) / 2, y: (maskView.cropRectView.frame.width - panImageView.frame.height) / 2)
    }
}
